namespace :mysql_import2 do 
  #按顺序执行
  #    mysql -u root -p 
  #>>  create database old_news;
  #>>  use old_news;
  #>>  source /db/test.sql;
  # rake db:create
  # rake db:migrate
  # rake  mysql_import:data
  # rake mysql_import:new
  # rake mysql_import:notify
  # rake db:seed
  # rake news_groups:old_news
  # rake category:update_categories
  # rake reports_notifies:update




  desc "通知导入"
  task :notify => :environment do 
    client = client_connection

    client.query("select * from data").each do |row|      
      notify = Notify.create(
        :id => row['id'],
        :title => row['title'],
        :content => row['content'],
        :hits => row['hits'],
        :fromwhere => row['fromwhere'],
        :postby => row['postby'],
        :writer => row['writer'],
        :created_at => get_datetime(row['addtime']),
        :deleted => get_datetime(row['gqsj'])
      )
      unless notify.valid?
        puts "=====notify....=id: #{row['id']}==="
        puts "errors:#{notify.errors.full_messages}"
      end
    end
    
    
    
        client.query("select *from report").each do |row|
       info = {
        :id => row['id'],
        :title => row['title'],
        :place => row['place'],
        :reporter => row['reporter'],
        :time => row['time'],
        :screen => row['screen'],
        :check => row['check'],
        :content => row["content"],
        :note => row['note'],
        :writer => row['writer'],
        :postby => row['postby'],
        :fromwhere => row['fromwhere'],
        :department => row['department']
      }     
      
      report = Report.create(info)
      unless report.valid?
        puts "=====report....=id: #{row['id']}==="
        puts "errors:#{report.errors.full_messages}"
      end
    end
    Report.find_by_sql("
      select setval('reports_id_seq', max(id)) from reports")
  end


  #报错 2006-05-00 14:16:01

  desc "新闻导入"
  task :new => :environment do
    client = client_connection

    client.query("update news set pubtime='2006-05-01 14:16:01' where  pubtime='2006-05-00 14:16:01'")

    create_row = lambda do |row|
      puts "==new=#{row['id']}========="
      category = nil
      category = Category.where(:id => row['cataid']).first if row['cataid'].present?

      NewsSptitle.create(:news_id => row['id'], :sptitle_id => row['sptitle']) if row['sptitle'].present?

      department = nil
      department = Department.where(:name => row['fromwhere']).first if row['fromwhere'].present?

      toback = nil
      if row["toback"].present?
        toback = Toback.where(:id => row["toback"].split("|").select(&:present?)[0])
      end

      edittime = row['edittime']
      if edittime.present?
        edittime = begin
          edittime.to_datetime
        rescue Exception => e
          nil
        end
      end

      pubtime = row['pubtime']
      if pubtime.present?
        pubtime = begin
          pubtime.to_datetime
        rescue Exception => e
          nil
        end
      end

      News.create(
        :id => row['id'],
        :category_id => category.try(:id),
        :is_important_news => row["sftj"],
        :title => row["title"],
        :bottomtitle => row["bottomtitle"],
        :uptitle => row["uptitle"],
        :author_name => row["writer"],
        :cameraman_name => row["photowriter"],
        :phone => row["phone"],
        :contribution_units => row['fromwhere'],
        :contribution_units_id => department.try(:id),
        :contribution_units_member => row["fromwhere2"],
        :writer => row["writer"],        
        :postby => row["postby"],
        :body => row['content'],
        :keywords => row['keywords'],
        :toback_reason => toback.try(:reason),
        :toback_count => row["backcount"],
        :check => row["check"],
        :edit => row["edit"],
        :hits => row["hits"],
        :pubwhere => row['pubwhere'],
        :puburl => row['puburl'],
        :pubversion => row['pubversion'],
        :pubtime => pubtime,
        :edittime => edittime ,
        :editby => row['editby'],
        :english => row['english']
      )
    end

    puts "========================1====================="
    client.query('select * from news').each do |row|
      create_row.call(row)
    end
   News.find_by_sql("
      select setval('news_id_seq', max(id)) from news")
  end
  
  
  
  

end